import java.util.Stack;

public class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;
    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if (minStack.empty()){
            minStack.push(val);
        }else {  //如果minStack 里面有值了就要判断 要存的val值跟minStack里面的val值谁的小就压谁的
            if(val <= minStack.peek()){
                minStack.push(val); // 去等是因为 ， 到时候minStack出栈的时候要伴随stack一起出栈
            }
        }
    }

    public void pop() {
        // 每次pop的值都要跟minStack的栈顶元素去比较
        if(!stack.empty()) {
            Integer val = stack.pop();
            if (val.equals(minStack.peek())) {
                minStack.pop();
            }
        }
    }


    //top就是peek
    public int top() {
        // 每次拿的是普通栈的栈顶元素
        if(!stack.empty()){
            return stack.peek();
        }
        return -1;
    }

    public int getMin() {
        //直接返回最小栈的栈顶元素
        return minStack.peek();
    }
}
